package edu.ciee.tz.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;

import edu.ciee.tz.model.Zhibie;

@Component("zhibieDao")
public class ZhibieDAO {

	public List<Zhibie> search(Zhibie z) throws Exception {

		String query = "where 1=1";

		if (z.getId() != 0) 
			query = query + " and ID like '%" + z.getId() + "%'";// this.getID()
		if (z.getName() != null) 
			query = query + " and name like '%" + z.getName() + "%'"; // this.getName()

		@SuppressWarnings("unchecked")
		List<Zhibie> zhibie_list = hibernateTemplate.find("from Zhibie u "+ query);

		return zhibie_list;
	}
	
	public List<Zhibie> searchAll() throws Exception {
		@SuppressWarnings("unchecked")
		List zhibie_list = hibernateTemplate.find("from Zhibie u ");
		
		Map map=new HashMap();
		System.out.println("size:"+zhibie_list.size());
		for(int i=0;i<zhibie_list.size();i++){
			Zhibie zb=new Zhibie();
			zb=(Zhibie) zhibie_list.get(i);
			System.out.println(zhibie_list.get(i));
			
			//HashMap m=zhibie_list.get(i);			
			//HashMap m=new HashMap();
			//m.put("id",zb.getId())	;
			//m.put("name", zb.getName());
			//map.put(m.get("id"), m.get("name"));
			//map.put("id",zb.getId());
			//map.put("name", zb.getName());
			map.put(zb.getId(), zb.getName());
		}			
		ActionContext.getContext().getSession().put("zhibieMap", map);
		
		return zhibie_list;
	}
	
	public void insert(Zhibie z) throws Exception {
		this.getHibernateTemplate().save(z);
	}

	public void delete(Zhibie z) throws Exception {
		this.getHibernateTemplate().delete(z);

	}

	public void update(Zhibie z) throws Exception {
		this.getHibernateTemplate().update(z);
	}

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
